﻿Imports System.Data.SqlClient
Public Class frmPhieuThue
    Dim cnStr As String = "Data Source=.\SQLEXPRESS;Initial Catalog=QLKS;Integrated Security=True"
    Dim con As New SqlConnection(cnStr)
    Dim mdel As frmPhongTrong.mydel 'nhan delegate tu frm Danh Muc Phong


    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
        Dim frm As New FrmDanhMucPhong
        frm.ShowDialog()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub TextBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
        Dim frm As New frmPhongTrong()
        frm.md = AddressOf myF
        frm.Show()
    End Sub

    Public Function myF(ByVal s As String) As String
        TextBox1.Text = s 'truyen du lieu tu frmDanhMucPhong vao textbox1 frmPhieuThue
        Return ""
    End Function



    Private Sub txtCMND_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCMND.Leave

        If (txtCMND.Text.Length < 9) Then
            MessageBox.Show("CMND ko được nhỏ hơn 9 ký tự!")
        Else

        End If
    End Sub

    Private Sub frmPhieuThue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LbNgay.Text = DateTime.Now
        loadcbx()
        loadGV()
        'insertNgayLapPhieu()
        checkGV()
    End Sub

    Private Sub loadcbx()
        Dim con As New SqlConnection(cnStr)
        Dim sql As String = "select * from LoaiKhach"
        Dim da As New SqlDataAdapter(sql, con)
        Dim dt As New DataTable()
        da.Fill(dt)
        cbxLoaiKhach.DataSource = dt
        cbxLoaiKhach.DisplayMember = "TenLoaiKhach"
        cbxLoaiKhach.ValueMember = "MaLoaiKhach"

    End Sub

    Private Sub loadGV()
        Dim sql As String = "select distinct MaPhieu,TenPhong,TenKhach,TenLoaiKhach,CMND,DiaChi,NgayBatDau,SoKhach,PhieuThue.MaLoaiKhach as MaLoaiKhach from PhieuThue,LoaiKhach where LoaiKhach.MaLoaiKhach=PhieuThue.MaLoaiKhach and NgayLapPhieu='" + Date.Now + "'"
        Dim bs As New BindingSource()
        Dim da As New SqlDataAdapter(sql, con)
        Dim dt As New DataTable()
        da.Fill(dt)
        bs.DataSource = dt
        DataGridView1.DataSource = bs
    End Sub


    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        If (TextBox1.Text <> "" And txtTenK.Text <> "") Then


            Dim sql As String = "insert into PhieuThue(TenPhong,TenKhach,MaLoaiKhach,CMND,DiaChi,NgayBatDau,SoKhach,NgayLapPhieu) values(@TenPhong,@TenKhach,@MaLoaiKhach,@CMND,@DiaChi,@NgayBatDau,@SoKhach,@NgayLapPhieu)" + "update Phong set TrangThai=@TrangThai where TenPhong=@TenPhong"
            Dim cmd As New SqlCommand(sql, con)

            cmd.Parameters.Add("@TenPhong", SqlDbType.NVarChar, 10)
            cmd.Parameters("@TenPhong").Value = TextBox1.Text

            cmd.Parameters.Add("@TenKhach", SqlDbType.NVarChar, 50)
            cmd.Parameters("@TenKhach").Value = txtTenK.Text

            cmd.Parameters.Add("@MaLoaiKhach", SqlDbType.Int, 10)
            cmd.Parameters("@MaLoaiKhach").Value = cbxLoaiKhach.SelectedValue

            cmd.Parameters.Add("@CMND", SqlDbType.Int)
            cmd.Parameters("@CMND").Value = txtCMND.Text

            cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 50)
            cmd.Parameters("@DiaChi").Value = txtDiaChi.Text

            cmd.Parameters.Add("@NgayBatDau", SqlDbType.DateTime, 20)
            cmd.Parameters("@NgayBatDau").Value = DateTimePicker1.Value

            cmd.Parameters.Add("@NgayLapPhieu", SqlDbType.Date, 20)
            cmd.Parameters("@NgayLapPhieu").Value = DateTime.Now

            cmd.Parameters.Add("@SoKhach", SqlDbType.Int, 10)
            cmd.Parameters("@SoKhach").Value = txtSlg.Text

            cmd.Parameters.Add("@TrangThai", SqlDbType.VarChar, 10)
            cmd.Parameters("@TrangThai").Value = "Đã Thuê"
            cmd.Connection.Open()

            Dim i As Integer = cmd.ExecuteNonQuery()
            If (i > 0) Then
                MessageBox.Show("Đã thêm 1 phiếu thuê phòng")
                clearAfterInsert()
            Else
                MessageBox.Show("Xảy ra lỗi!(Mỗi ngày chỉ được tối đa 3 khách)")
            End If
            cmd.Connection.Close()
            loadGV()
            If (DataGridView1.RowCount > 3) Then
                DataGridView1.AllowUserToAddRows = False
                btnThem.Enabled = False
            End If
        Else
            MessageBox.Show("Chưa nhập đủ thông tin")
        End If
    End Sub
    Private Sub checkGV()
        If (DataGridView1.RowCount > 3) Then
            DataGridView1.AllowUserToAddRows = False
            btnThem.Enabled = False
            TextBox1.Enabled = False
            txtCMND.Enabled = False
            txtDiaChi.Enabled = False
            txtTenK.Enabled = False
            cbxLoaiKhach.Enabled = False
            DateTimePicker1.Enabled = False
            txtSlg.Enabled = False
        Else
            btnThem.Enabled = True
            DataGridView1.AllowUserToAddRows = True
        End If
    End Sub
    
#Region " dgvContainer_RowsRemoved"
    Private Sub dgvContainer_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
        If DataGridView1.RowCount < 3 Then
            DataGridView1.AllowUserToAddRows = True
        End If
    End Sub
#End Region

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If (e.RowIndex >= 0 And e.ColumnIndex >= 0 And e.ColumnIndex <> Nothing) Then
            txtTenK.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("TenKhach").Value)
            cbxLoaiKhach.SelectedValue = DataGridView1.CurrentRow.Cells("MaLoaiKhach").Value
            txtCMND.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("CMND").Value)
            txtDiaChi.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("DiaChi").Value)
            TextBox1.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("TenPhong").Value)
            txtSlg.Text = DataGridView1.CurrentRow.Cells("SoKhach").Value
            DateTimePicker1.Value = Convert.ToDateTime(DataGridView1.CurrentRow.Cells("NgayBatDau").Value)
        End If
    End Sub


    Private Sub clearAfterInsert()
        TextBox1.Text = String.Empty
        txtTenK.Text = String.Empty
        txtDiaChi.Text = String.Empty
        txtCMND.Text = String.Empty
    End Sub
    
   
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New SuaPhieuThue()
        frm.Show()
    End Sub

    Private Sub txtDiaChi_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtDiaChi.MouseClick
        If txtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            txtCMND.Focus()
        End If
    End Sub

    Private Sub txtTenK_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtTenK.MouseClick
        If txtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            txtCMND.Focus()
        End If
    End Sub

    Private Sub txtSlg_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtSlg.MouseClick
        If txtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            txtCMND.Focus()
        End If
    End Sub

    Private Sub cbxLoaiKhach_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbxLoaiKhach.MouseClick
        If txtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            txtCMND.Focus()
        End If
    End Sub

    Private Sub DateTimePicker1_MouseCaptureChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.MouseCaptureChanged
        If txtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            txtCMND.Focus()
        End If
    End Sub

    
End Class